package org.fuyd.designpattern.entity;

import com.alibaba.fastjson.JSON;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

/**
 * Hello world!
 */
public class App {

    static String txt2String(File file) {
        String result = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result = result + "\n" + s;
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
        File file = new File("D:/广州.json");
        File file1 = new File("D:/广州地铁.json");
        String str = txt2String(file);

        String s = txt2String(file1);

        List<District> guangzhou = JSON.parseArray(str, District.class);

//        for (District district : guangzhou) {
//            List<Area> areas = district.getT();
//
//            for (Area area : areas) {
//
//                System.out.println("insert into pms_region ('id', 'letter', 'level', 'name', 'parent', 'type') values ("
//                                + area.getId() + "," + getFirstLetter(area.getD().substring(0, 1).charAt(0)).substring(0, 1).toUpperCase() +", 4, " + area.getD() + "," + district.getId() + "," + "分区);"
//
//
//                );
//            }
//
//        }

        List<MetroLine> metroLines = JSON.parseArray(s, MetroLine.class);

        for (MetroLine metroLine : metroLines) {
            List<MetroStation> metroStations = metroLine.getMetroStationInfoList();

            for (MetroStation metroStation : metroStations) {
                System.out.println( metroStation.getName() + "  " + (Integer.valueOf(metroLine.getCode())+6) + "   " + metroStation.getName());
            }

           // System.out.println(metroLine.getCityCode() + " " + metroLine.getName() + " " + metroLine.getCode());
        }

    }


    public static String getFirstLetter(char s) throws BadHanyuPinyinOutputFormatCombination {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(s, format);

        return pinyinArray[0];
    }
}